require 'gzoom_control'

class MapController < ApplicationController
  layout 'default'
  
  def index
    @map = current_map
    @locations = Location.find(:all)
    @location = @map.location
    @gmap = GMap.new("map_div")
    @gmap.control_init(:large_map => true, :map_type => true, 
                      :scale => true, :overview_map => true)
    @gmap.interface_init(:double_click_zoom => true,
                        :continuous_zoom => true)
    @gmap.set_map_type_init(GMapType::G_HYBRID_MAP)
    @gmap.record_init @gmap.add_control(GZoomControl.new({}, { :bStickyZoom => false,
              :sButtonHTML => "<img src='/images/zoom-button.gif' />",
              :sButtonZoomingHTML => "<img src='/images/zoom-button-activated.gif' />",
              :oButtonStartingStyle => {:width => '24px', :height => '24px'} }))
    @gmap.center_zoom_init([@map.x, @map.y], @map.zoom)
    @gmap.record_init @gmap.on_moveend("on_moveend")
    @gmap.record_init @gmap.on_mousemove("on_mousemove")
    Style.find(:all).each do |style|
      icon = GIcon.new(:image => "/images/icons/#{style.icon}", 
                       :icon_size => GSize.new(style.width, style.height),
                       :icon_anchor => GPoint.new(15,15),
                       :info_window_anchor => GPoint.new(19,4))
      @gmap.icon_global_init(icon, "style#{style.id}")
    end
    markers_by_style = Shape.find(:all, :include => [:layer, :shape_attributes ]).inject({}) do |h, shape|
      style_str = "style#{shape.layer.style.id}"
      h[style_str] ||= []
      h[style_str] << GMarker.new([shape.x, shape.y], :info_window => info_window(shape),
                  :title => shape.title, :icon => Variable.new(style_str))
      h
    end
    markers_by_style.each do |style_str, markers| 
      @gmap.overlay_init Clusterer.new(markers, :icon => Variable.new(style_str))
    end
  end

  def info_window(shape)
    str = "<table border='1'>"
    shape.shape_attributes.each do |attr|
      str << "<tr><td align='right'>#{attr.name}</td><td>#{attr.value}</td></tr>"
    end
    str << "<table>"
    str
  end

  def move
    self.current_map = current_map.moved_to(params.keep(:x, :y, :zoom))
    @location = current_map.location
  end

  def back
    navigate(current_map.previous)
  end
  
  def forward
    navigate(current_map.next)
  end

  def goto
    location = Location.find(params[:id])
    navigate(location.map)
  end

  def navigate(map)
    if @map = map
      self.current_map = map 
      @gmap = Variable.new("map")
      render :template => 'map/navigate'
    else
      render :nothing => true
    end
  end
  
  def save
    @location = Location.new(:title => params[:title])
    @location.map = current_map
    @location.save
    @locations = Location.find(:all)
  end
    
  def current_map=(map)
    @current_map = map
    session[:current_map_id] = map.id
  end

  def current_map
    @current_map ||= Map.find(session[:current_map_id].to_i) rescue last_or_default_map
  end
  
  def last_or_default_map
    unless last = Map.last
      last = Map.default
      last.save
    end
    last
  end
end